<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of test
 *
 * @author Gidaff_01
 */
class Test extends Admin_Controller{
    //put your code here
    function Test() {
        parent::Admin_Controller();
        $this->load->model('Test_m');
        $this->load->model('Commandes_m');
        $this->load->library('pagination');
        $this->load->library('session');
        $this->load->helper('date');
        $this->_data['erreur'] = "";
    }

    function index() {
        $this->liste_test();
    }

    function liste_test(){
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/test/liste_test/' . '/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Test_m->count_all_test();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['product_test'] = $this->Test_m->get_all_test($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Test_m->count_all_test();
        $this->_data['page_title'] = "Liste de test";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('product_test/liste_test');
    }

    function delete_test($id, $per_page, $off_set) {
        $this->Test_m->delete_test($id);
        redirect('test/liste_test' . '/' . $per_page . '/' . $off_set);
    }

    function detail_test($id){
        is_admin();
        $this->_data['page_title'] = "Detail produit test";
        $test = $this->Test_m->get_test_detail($id);
        $this->_data['detail_test'] = $test;
        $this->display_view('product_test/detail_test');
    }

    function delivered($test_id,$user_id) {
        is_admin();
        $this->load->library('email');
        $user_data = $this->Commandes_m->get_user_by_id($user_id);
        if ($user_data->num_rows() > 0) {
            $user = $user_data->row();

            $this->email->from('administrator@lehu.com', 'Zoo-market');
            $this->email->to($user->email);
            $this->email->subject('Confirmer la livraison');
            $message = 'Hello ' . $user->display_name;
            $message .= '. Nous avons été la livraison du test produit que vous commandez sur notre site web';
            $this->email->message($message);
//            echo $user->email;
//            echo $user->display_name;
//            echo $message;die;
            $this->email->send();

            $this->Test_m->delivered($test_id);
            redirect(site_url('test'));
        }  
        $this->Test_m->delivered($test_id);
        redirect(site_url('test'));
    }
}
?>
